import time
import requests
from bs4 import BeautifulSoup
import os
import sys
import hashlib

currentTime = time.time()
def md5_encrypt(input_string):  
    # 创建一个md5对象  
    md5_hash = hashlib.md5()  
      
    # 提供需要加密的字符串，必须是bytes类型  
    # 如果输入的是字符串，需要先编码为bytes  
    if not isinstance(input_string, bytes):  
        input_string = input_string.encode()  
          
    # 使用update方法传入字符串进行加密  
    md5_hash.update(input_string)  
      
    # 使用hexdigest方法获取加密后的16进制字符串  
    hex_dig = md5_hash.hexdigest()  
      
    return hex_dig  


class Spider:
    url = "https://www.baidu.com"
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) "
                      "Version/16.1 Safari/605.1.15 ",
    }

    def __init__(self, url, folderPath):
        self.url = url
        self.folderPath = folderPath

    def getImage(self, url, name):
        response = requests.get(url, headers=self.headers)
        if response.status_code == 200:
            content_type = response.headers.get('Content-Type')
            image_format = "jpg"
            if content_type:
                image_format = content_type.split('/')[1]
            with open(f"./{self.folderPath}/{name}.{image_format}", "wb") as image:
                for chunk in response.iter_content(1024):
                    if chunk:
                        image.write(chunk)
                        image.flush()

    def start(self):
        response = requests.get(self.url, headers=self.headers)
        if response.ok:
            soup = BeautifulSoup(response.text, "lxml")
            imgs = soup.select("img")
            url_array = []
            name_array = []
            for img in imgs:
                if 'src' in img.attrs:
                    if img.attrs["src"].startswith("/"):
                        url_array.append(f"https:{img.attrs['src']}")
                    else:
                        url_array.append(img.attrs['src'])
                    if 'alt' in img.attrs:
                        name_array.append(img.attrs['alt'])
                    else:
                        name_array.append(time.time())
            for url, name in zip(url_array, name_array):
                self.getImage(url, name)  
            print(len(url_array))
            print(time.time() - currentTime)
            print(self.url)

  

site = sys.argv[1]
folderPath = md5_encrypt(site)

if not os.path.exists(f'./{folderPath}'):
    os.mkdir(f'./{folderPath}')
    site = sys.argv[1] 
    spider = Spider(site, folderPath)
    spider.start()
    
